package com.oha.myutils.commons;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class Util {

    /**
     * Transforma un DTO a cadena.
     * @param obj
     * @return
     * @throws SecurityException 
     * @throws NoSuchMethodException 
     */
    public static synchronized String dtoToString(Object obj){
    	if(obj!=null){
    		StringBuilder out = new StringBuilder();
        	out.append("[");
        	Field[] props = obj.getClass().getDeclaredFields();
        	for(Field prop:props){
    			try{
    				String methodName= prop.getType().equals(Boolean.class) ? "is" : "get";
    	    		methodName += String.valueOf(prop.getName().charAt(0)).toUpperCase();
    				methodName += prop.getName().substring(1);
    				Method getter = obj.getClass().getMethod(methodName);
    				Object value = getter.invoke(obj, new Object[0]);
    	    		out.append(prop.getName()+":"+value);
    	    		out.append(", ");
    			}catch(Exception e){
    			}    		
        	}
        	out.append("]");
        	return out.toString();
    	}else{
    		return "No dto object";
    	}
    }
	
}
